package com.gxa.xc.rbac.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 登陆的拦截器
 * @author:IT-CNLM
 * @date:Created at 2020/10/22
 */
public class LoginInterceptor implements HandlerInterceptor {


    /**
     *  拦截前
     * @param request
     * @param response
     * @param handler
     * 关于是否放行的判断机制
     *      返回false  表示拦截不放行
     *      返回true  放行
     *
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        // 判断当前这个请求是否有session存在
        // 先获取session对象
        HttpSession session = request.getSession();
        String adminName = (String) session.getAttribute("adminName");
        if (adminName == null) {
            // 说明没有登陆的, 重定向到登陆页面
            response.sendRedirect("/login/page");
            return false;
        }
        // 放行
        return true;
    }

    /**
     * 拦截后
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    /**
     * 视图渲染完成之后调用的方法
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
